package dal;

import infrastructure.configuration.Configuration;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPAAgent {

	private static volatile JPAAgent _theInstance = null;
	
	private final EntityManagerFactory factory;
	
	
	protected JPAAgent(){		
		Properties props = new Properties();
		props.put("openjpa.jdbc.SynchronizeMappings","buildSchema");
		props.put("openjpa.ConnectionURL",Configuration.getInstance().getDBFilePath());
		props.put("openjpa.ConnectionDriverName","com.mysql.jdbc.Driver");		
		props.put("openjpa.ConnectionUserName","root");
		props.put("openjpa.ConnectionPassword","scorpions");
		
				 
		factory = Persistence.createEntityManagerFactory("pets", props);
	}
	
	public static JPAAgent getInstance(){
		
		if (null == _theInstance){
			synchronized (JPAAgent.class) {
				
				// double check 
				if (null == _theInstance){
					_theInstance =  new JPAAgent();
				}
			}
		}
		
		return _theInstance;
	}
	
	
	public EntityManagerFactory getEntityManagerFactory(){
		return factory;
	}
}
